package com.bort.system.controller.client;

import com.bort.system.BortApplication;
import com.bort.system.common.DataContainer;
import com.bort.system.service.TDeskService;
import com.bort.system.info.DeskInfo;
import com.bort.system.view.IndexView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.application.HostServices;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.DialogPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

@FXMLController
public class MainAppController  implements Initializable {

    Stage dialogStage;

    @Resource
    TDeskService deskService;

    @FXML
    FlowPane rootPane;

    @FXML
    void onExitClick(){
        Stage mainApp = BortApplication.getStage();
        mainApp.close();
        BortApplication.showView(IndexView.class);
    }

    @FXML
    void toBrowser(){
        HostServices services = BortApplication.getAppHostServices();
        services.showDocument("http://127.0.0.1:9880/index");
    }

    @FXML
    void onHelpClick(){
        Stage stage = BortApplication.getStage();
        DialogPane dialogPane = new DialogPane();
        dialogPane.setContentText("开发着联系电话: 18902123716");
        dialogPane.setPrefWidth(320);
        dialogPane.setPrefHeight(90);
        if(dialogStage == null){
            dialogStage = new Stage();
        }
        dialogStage.setY(stage.getY() + 60);
        dialogStage.setX(stage.getX() + 120);
        Scene scene = new Scene(dialogPane);
        dialogStage.setScene(scene);
        dialogStage.show();
        dialogStage.setIconified(false);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        BortApplication.getStage().setResizable(true);
        rootPane.setStyle("-fx-background-color: white");
        if(DataContainer.deskContainer == null){
            DataContainer.deskContainer = new HashMap<>();
        }
        if(DataContainer.deskCtrlMap == null){
            DataContainer.deskCtrlMap = new HashMap<>();
        }
        List<DeskInfo> list = deskService.getAllDesk();
        for (DeskInfo tDesk : list) {
            initData(tDesk);
        }
    }

    void initData(DeskInfo tDesk){
       try{
           DataContainer.deskIndex = tDesk.getId();
           DataContainer.deskContainer.put(tDesk.getId(),tDesk);
           FXMLLoader fxmlLoader = new FXMLLoader(MainAppController.class.getResource("/view/deskView.fxml"));
           AnchorPane root = (AnchorPane)fxmlLoader.load();
           rootPane.getChildren().add(root);
       }catch (IOException e){
           e.printStackTrace();
       }
    }

}
